package com.example.xd.default_call

import android.content.Intent
import android.telecom.Call
import android.telecom.InCallService
import android.telecom.VideoProfile
import com.example.xd.default_call.Global_Val.Companion.CALL_IN
import com.example.xd.default_call.Global_Val.Companion.CALL_OUT
import com.example.xd.default_call.Global_Val.Companion.CALL_VIDEO_IN
import com.example.xd.default_call.Global_Val.Companion.CALL_VIDEO_OUT
import com.example.xd.default_call.Global_Val.Companion.MULT_CALL_IN
import com.example.xd.default_call.eventbus_msg.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.*

/**
 * @auther XuDan on 2020/8/13.
 */
class UIService : InCallService() {
    companion object {
        val call_list = LinkedList<Call>()
    }

    override fun onCreate() {
        super.onCreate()
        EventBus.getDefault().register(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    override fun onCallAdded(call: Call) {
        super.onCallAdded(call)
        call_list.add(call)
        val state = call.state
        when (state) {
            Call.STATE_RINGING -> {//来电
                when (call_list.size) {
                    1 -> {
                        val videoState = call.details.videoState
                        when (videoState) {
                            VideoProfile.STATE_AUDIO_ONLY -> {
                                val intent = Intent(UIService@ this, UIActivity::class.java)
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                                startActivity(intent)
                                EventBus.getDefault().postSticky(InitFragmentMsg(CALL_IN))
                                EventBus.getDefault().postSticky(CallInSatrtMsg(call))
                            }
                            VideoProfile.STATE_BIDIRECTIONAL -> {
                                val intent = Intent(UIService@ this, UIActivity::class.java)
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                                startActivity(intent)
                                EventBus.getDefault()
                                    .postSticky(InitFragmentMsg(CALL_VIDEO_IN))
                                EventBus.getDefault()
                                    .postSticky(VideoCallInMsg(call))
                            }
                        }
                    }
                    2 -> {
                        val videoState = call.details.videoState
                        when (videoState) {
                            VideoProfile.STATE_AUDIO_ONLY -> {
                                EventBus.getDefault()
                                    .post(MultCallInMsg(MULT_CALL_IN))
                            }
                            VideoProfile.STATE_BIDIRECTIONAL -> {
                            }
                        }
                    }
                }


            }
            Call.STATE_CONNECTING -> {//去电
                val videoState = call.details.videoState
                when (videoState) {
                    VideoProfile.STATE_AUDIO_ONLY -> {
                        val intent = Intent(UIService@ this, UIActivity::class.java)
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        startActivity(intent)
                        EventBus.getDefault().postSticky(InitFragmentMsg(CALL_OUT))
                        EventBus.getDefault().postSticky(CallOutStartMsg(call))
                    }
                    VideoProfile.STATE_BIDIRECTIONAL -> {
                        val intent = Intent(UIService@ this, UIActivity::class.java)
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        startActivity(intent)
                        EventBus.getDefault().postSticky(InitFragmentMsg(CALL_VIDEO_OUT))
                        EventBus.getDefault()
                            .postSticky(VideoCallOutMsg(call))
                    }
                }
            }
        }


    }

    override fun onCallRemoved(call: Call) {
        super.onCallRemoved(call)
        call_list.remove(call)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun setIsSlience(msg: IsSlienceMsg) {
        setMuted(msg.slience)
    }
}